package com.test.springboot.springboot3testdemo3.controller;

import com.test.springboot.springboot3testdemo3.entity.User;
import com.test.springboot.springboot3testdemo3.service.JetCacheUserService;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadPoolExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： liuziyang
 * @date： 2024/9/14-10:19
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@RestController
public class JetCacheUserController {
  private static final List<Integer> IDS = new ArrayList<>();
  private final JetCacheUserService jetCacheUserService;

  @Autowired
  @Qualifier("threadPoolExecutor")
  private ThreadPoolExecutor executor;

  public JetCacheUserController(JetCacheUserService jetCacheUserService) {
    this.jetCacheUserService = jetCacheUserService;
  }

  @GetMapping("save")
  public User save() {
    int id = new Random().nextInt(10000000);
    IDS.add(id);
    User user = new User(id, "张三", new Random().nextInt(100));
    executor.execute(() -> jetCacheUserService.save(user));
    return user;
  }

  @GetMapping("/{id}")
  public User get(@PathVariable Integer id) {
    return this.jetCacheUserService.get(id);
  }

  @GetMapping("/update/{id}")
  public User update(@PathVariable Integer id) {
    User user = new User(id, "张三", new Random().nextInt(100));
    return this.jetCacheUserService.updateUser(user);
  }

  @GetMapping("/delete/{id}")
  public void delete(@PathVariable Integer id) {
    IDS.remove(id);
    this.jetCacheUserService.deleteUser(id);
  }
}
